package com.cskaoyan.market.service.Impl;

import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.db.domain.MarketBrandExample;
import com.cskaoyan.market.db.mapper.MarketBrandMapper;
import com.cskaoyan.market.service.MarketBrandService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName: MarketBrandServiceImpl
 * @Description: MarkerBrandService的实现类
 * @Author: 鲸落
 * @Date: 2024/05/06/15:18
 * @Version: 1.0
 */
@Service
public class MarketBrandServiceImpl implements MarketBrandService {
    @Autowired
    MarketBrandMapper brandMapper;
    @Override
    public List<MarketBrand> list(Integer page, Integer limit, String id, String name, String sort, String order) {
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        // 设定排序规则   此处有sql注入风险，因为接收了用户的输入，需要手动处理 todo
        marketBrandExample.setOrderByClause(sort+" "+order);
        // 通过criteria设定sql语句条件
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        if(!StringUtils.isEmpty(id)){
            criteria.andIdEqualTo(Integer.parseInt(id));  // id=
        }
        if(!StringUtils.isEmpty(name)){ // name=
            criteria.andNameEqualTo(name);
        }
        // 开启分页
        PageHelper.startPage(page,limit);
        return brandMapper.selectByExample(marketBrandExample);
    }

    @Override
    public MarketBrand createBrandMerchants(String name, String desc, String floorPrice, String picUrl) {
        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setName(name);
        marketBrand.setDesc(desc);
        marketBrand.setFloorPrice(new BigDecimal(floorPrice));
        marketBrand.setPicUrl(picUrl);
        marketBrand.setAddTime(LocalDateTime.now());
        marketBrand.setUpdateTime(LocalDateTime.now());
        brandMapper.insertSelective(marketBrand);
        return marketBrand;
    }

    @Override
    public Integer deleteBrandMerchantsById(Integer id) {
        return brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public MarketBrand updateBrandMerchants(MarketBrand marketBrand) {
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        marketBrand.setUpdateTime(LocalDateTime.now());
        // 根据Id去更新指定数据
        criteria.andIdEqualTo(marketBrand.getId());
        brandMapper.updateByExampleSelective(marketBrand,marketBrandExample);
        return marketBrand;
    }
}
